package com.zyh.service.impl;

import com.zyh.entity.Resource;
import com.zyh.dao.ResourceMapper;
import com.zyh.entity.ResultEntity;
import com.zyh.service.IResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单管理 服务实现类
 * </p>
 *
 * @author zhang
 * @since 2022-05-08
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements IResourceService {
    @Override
    public List<Resource> selectMenu() {
        List<Resource> resources = baseMapper.selectList(null);//查出所有菜单
        List<Resource> collect = resources.stream()
                .filter((menu) -> menu.getType() == 0)//过滤出一级菜单
                .peek((menu)->menu.setResourceList(selectChild(menu.getMenuId(),resources)))//给一级分类的子菜单属性赋值。
                .collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<Resource> selectChild(Long parentId, List<Resource> resources) {
        return resources.stream().filter(menu->menu.getParentId().equals(parentId))//根据父ID过滤出对应的子菜单。
                //给当前子菜单的子菜单属性赋值
                .peek(menu->menu.setResourceList(selectChild(menu.getMenuId(),resources)))//递归直至最终向上返回。
                .collect(Collectors.toList());
    }

    @Override
    public List<Resource> selectResourcesByAdminId(Integer adminId) {
        return baseMapper.selectResourcesByAdminId(adminId);
    }
}
